<template>
  <div class="execution">
    <dynamic-crud
      :rowAdd="rowadd"
      :data="tableDataCashThis"
      :option="tableOptionCash"
      title="现金支付"
      :isShow="oneShow"
      :rowOneButton="oneButton"
      :addNew="rowNew"
      :hiddened="myType == '1'"
      :addDisabled="myType == '1' && tableDataCashThis.length > 0"
    ></dynamic-crud>
    <el-form>
      <el-form-item label="备注">
        <el-input type="textarea" v-model="remark" :rows="5"></el-input>
      </el-form-item>
    </el-form>
    <div class="bottom-menu">
      <el-button
        type="primary"
        :disabled="btn"
        v-if="myType == '0'"
        @click="save"
        >保存</el-button
      >
      <el-button type="primary" v-if="myType == '1'" @click="update"
        >保存</el-button
      >
      <el-button type="primary" @click="closeDia()">取消</el-button>
    </div>
  </div>
</template>

<script>
import {
  saveObj,
  updateObj,
  getTvmCode
} from "@/api/subwaystm/swsttvmdailybill/swsttvmdailybill";
import { tableOptionCash } from "@/const/crud/subwaystm/swsttvmdailybill/swsttvmdailybillcashpay";
import { mapGetters } from "vuex";
import store from "@/store";
import { dateFormat } from "@/util/date";
export default {
  name: "swsttvmdailybillcashpay",
  inject: ["closeDialog"],
  data() {
    return {
      searchForm: {},
      tableDataCashThis: [],
      btn: false,
      oneShow: true,
      rowNew: true,
      tvmList: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 // 每页显示多少条
      },
      tableLoading: false,
      //tableOptionCash: tableOptionCash,
      remark: "",
      formAdd: {
        swStTvmDailyBill: {},
        swStTvmDailyBillCashPays: []
      }
    };
  },
  props: {
    cashData: {
      type: Array,
      default: function() {
        return [];
      }
    },
    myType: String,
    tvmCode: String
  },
  watch: {
    cashData() {
      this.tableDataCashThis = this.cashData;
    }
    // tableDataCashThis: {
    //   handler(newValue, oldValue) {
    //     for (let i = 0; i < newValue.length; i++) {
    //       newValue[i].receiveTotalMoney = 0;
    //       newValue[i].changeTotalMoney = 0;
    //       //接收总计计算
    //       if (newValue[i].receiveCoin && newValue[i].receiveNotes) {
    //         newValue[i].receiveTotalMoney =
    //           newValue[i].receiveCoin + newValue[i].receiveNotes;
    //       }
    //       if (newValue[i].receiveCoin && newValue[i].receiveNotes == 0) {
    //         newValue[i].receiveTotalMoney =
    //           newValue[i].receiveCoin + newValue[i].receiveNotes;
    //       }
    //       if (newValue[i].receiveCoin == 0 && newValue[i].receiveNotes) {
    //         newValue[i].receiveTotalMoney =
    //           newValue[i].receiveCoin + newValue[i].receiveNotes;
    //       }
    //       if (newValue[i].receiveCoin == 0 && newValue[i].receiveNotes == 0) {
    //         newValue[i].receiveTotalMoney =
    //           newValue[i].receiveCoin + newValue[i].receiveNotes;
    //       }
    //       /*if(isNaN(newValue[i].receiveTotalMoney)){
    //             newValue[i].receiveTotalMoney = 0
    //           }*/
    //       //找零总计计算
    //       if (newValue[i].noteChangen && newValue[i].coinChange) {
    //         newValue[i].changeTotalMoney =
    //           newValue[i].noteChangen + newValue[i].coinChange;
    //       }
    //       if (newValue[i].noteChangen && newValue[i].coinChange == 0) {
    //         newValue[i].changeTotalMoney =
    //           newValue[i].noteChangen + newValue[i].coinChange;
    //       }
    //       if (newValue[i].noteChangen == 0 && newValue[i].coinChange) {
    //         newValue[i].changeTotalMoney =
    //           newValue[i].noteChangen + newValue[i].coinChange;
    //       }
    //       if (newValue[i].noteChangen == 0 && newValue[i].coinChange == 0) {
    //         newValue[i].changeTotalMoney =
    //           newValue[i].noteChangen + newValue[i].coinChange;
    //       }
    //     }
    //   },
    //   deep: true,
    //   immediate: true
    // }
  },
  computed: {
    ...mapGetters(["permissions"]),
    permissionList() {
      return {
        addBtn: this.vaildData(
          this.permissions.generator_swsttvmdailybillcashpay_add,
          false
        ),
        delBtn: this.vaildData(
          this.permissions.generator_swsttvmdailybillcashpay_del,
          false
        ),
        editBtn: this.vaildData(
          this.permissions.generator_swsttvmdailybillcashpay_edit,
          false
        )
      };
    },
    tableOptionCash() {
      let option = this.deepClone(tableOptionCash);
      this.findObject(option.column, "tvmCode").cell = this.myType == "0";
      if (this.oneShow && this.myType == "0") {
        this.findObject(option.column, "tvmCode").cell = false;
      }
      return option;
    }
  },
  beforeMount() {
    getTvmCode().then(res => {
      this.tvmList = res.data.data.records;
    });
  },
  methods: {
    rowadd: function(callback) {
      this.oneShow = false;
      this.tableOptionCash.column[2].cell = true;
      let startTime = new Date();
      let endTime = new Date();
      if (startTime.getHours() < 6) {
        startTime.setDate(startTime.getDate() - 1);
      }
      if (endTime.getHours() >= 6) {
        endTime.setDate(endTime.getDate() + 1);
      }
      //endTime.setDate(startTime.getDate() + 1);
      if (this.tableDataCashThis.length > 0) {
        callback({
          startTime: dateFormat(startTime).substr(0, 10) + " 02:00:00",
          endTime: dateFormat(endTime).substr(0, 10) + " 01:59:59"
        });
      } else {
        if (this.myType == "0") {
          callback({
            startTime: dateFormat(startTime).substr(0, 10) + " 02:00:00",
            endTime: dateFormat(endTime).substr(0, 10) + " 01:59:59"
          });
        } else {
          callback({
            tvmCode: this.tvmCode,
            startTime: dateFormat(startTime).substr(0, 10) + " 02:00:00",
            endTime: dateFormat(endTime).substr(0, 10) + " 01:59:59"
          });
        }
      }
    },
    oneButton: function(callback) {
      this.rowNew = false;
      this.tableOptionCash.column[2].cell = false;
      if (this.tableDataCashThis.length > 0) {
        this.$confirm("此操作将重新生成, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          for (const item of this.tableDataCashThis) {
            Object.keys(item).map(key => {
              if (key != "$cellEdit" && key != "tvmCode") {
                item[key] = undefined;
              }
            });
          }
        });
      } else {
        for (const item of this.tvmList) {
          const obj = Object.assign({}, item);
          let startTime = new Date();
          let endTime = new Date();
          if (startTime.getHours() < 6) {
            startTime.setDate(startTime.getDate() - 1);
          }
          if (endTime.getHours() >= 6) {
            endTime.setDate(endTime.getDate() + 1);
          }
          (obj.startTime = dateFormat(startTime).substr(0, 10) + " 02:00:00"),
            (obj.endTime = dateFormat(endTime).substr(0, 10) + " 01:59:59");
          callback(obj);
        }
      }
    },
    save: function() {
      let tvmCodeList = [];
      if (this.tableDataCashThis.length == 0) {
        this.$message.info("请添加现金支付录入信息！");
        return;
      } else {
        for (const item of this.tableDataCashThis) {
          tvmCodeList.push(item.tvmCode);
          if (
            item.tvmCode == null ||
            item.sellTicketTotalMoney == null ||
            item.rechargeMoney == null ||
            item.startTime == null ||
            item.endTime == null
          ) {
            this.$message.info(
              "TVM编号、售票总计、充值总计、开始时间、结束时间为必填项！不能为空"
            );
            return;
          }
          if(item.receiveNotes!==undefined){
            if(String(item.receiveNotes).length>4){
              this.$message.info(
                "请检查接收纸币金额不能超过4位数！"
              );
              return;
            }
          }
          if(item.receiveCoin!==undefined){
            if(String(item.receiveCoin).length>4){
              this.$message.info(
                "请检查接收硬币金额不能超过4位数！"
              );
              return;
            }
          }
          if(item.noteChangen!==undefined){
            if(String(item.noteChangen).length>4){
              this.$message.info(
                "请检查纸币找零金额不能超过4位数！"
              );
              return;
            }
          }
          if(item.coinChange!==undefined){
            if(String(item.coinChange).length>4){
              this.$message.info(
                "请检查硬币找零金额不能超过4位数！"
              );
              return;
            }
          }
          if(item.faultRefundAmount!==undefined){
            if(String(item.faultRefundAmount).length>4){
              this.$message.info(
                "请检查故障退款金额不能超过4位数！"
              );
              return;
            }
          }
          if(item.sellTicketTotalMoney!==undefined){
            if(String(item.sellTicketTotalMoney).length>4){
              this.$message.info(
                "请检查售票总计金额不能超过4位数！"
              );
              return;
            }
          }
          if(item.rechargeMoney!==undefined){
            if(String(item.rechargeMoney).length>4){
              this.$message.info(
                "请检查充值总计金额不能超过4位数！"
              );
              return;
            }
          }
        }
        var s = tvmCodeList.join(",")+",";
        for(var i=0;i<tvmCodeList.length;i++){
          if (s.replace(tvmCodeList[i]+",","").indexOf(tvmCodeList[i]+",")>-1){
            this.$message.info(
              "请检查新增的数据中不能有相同TVM编号的数据！"
            );
            return;
          }
        }
      }
      this.formAdd.swStTvmDailyBillCashPays = this.tableDataCashThis;
      this.formAdd.swStTvmDailyBill.startTime = this.tableDataCashThis[0].startTime;
      this.formAdd.swStTvmDailyBill.endTime = this.tableDataCashThis[0].endTime;
      this.formAdd.swStTvmDailyBill.remark = this.remark;
      this.formAdd.swStTvmDailyBill.operatorName =
        store.getters.userInfo.nikeName;
      this.formAdd.swStTvmDailyBill.operatorJobNo =
        store.getters.userInfo.userCode;

      saveObj(this.formAdd)
        .then(data => {
          this.$message.success("添加成功");
          this.btn = true;
          done();
        })
        .catch(() => {});
    },

    update: function() {
      if (this.tableDataCashThis.length == 0) {
        this.$message.info("请添加现金支付录入信息！");
        return;
      } else {
        for (const item of this.tableDataCashThis) {
          if (
            item.tvmCode == null ||
            item.sellTicketTotalMoney == null ||
            item.rechargeMoney == null ||
            item.startTime == null ||
            item.endTime == null
          ) {
            this.$message.info(
              "TVM编号、售票总计、充值总计、开始时间、结束时间为必填项！不能为空"
            );
            return;
          }
        }
      }
      this.formAdd.swStTvmDailyBillCashPays = this.tableDataCashThis;
      this.formAdd.swStTvmDailyBill.startTime = this.tableDataCashThis[0].startTime;
      this.formAdd.swStTvmDailyBill.endTime = this.tableDataCashThis[0].endTime;
      this.formAdd.swStTvmDailyBill.remark = this.remark;
      saveObj(this.formAdd)
        .then(data => {
          this.$message.success("修改成功");
          this.btn = true;
          this.closeDialog();
          done();
        })
        .catch(() => {});
    },
    closeDia() {
      this.$confirm("请确认是否已将数据保存?", "提示", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.closeDialog();
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.bottom-menu {
  margin-top: 48px;
  text-align: center;
}
</style>
